package com.tr.rentcar.controller.houtai.log;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.bean.Admin_he;
import com.tr.rentcar.model.dao.AdminDao;;

@WebServlet("/houtaiadminlogin")
public class AdminlogincontrolerController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/jsp/houtai_adminlogin.jsp").forward(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			String aname = req.getParameter("aname");
			String apassword = req.getParameter("apassword");
			String code = req.getParameter("yanzhengma");

			String yan_code = (String) req.getSession().getAttribute(CaptchaController.CAPTCHA_KEY);
			if (StringUtils.isEmpty(aname)) {
				req.setAttribute("error", "用户名为空");
				doGet(req, resp);
				return;
			}
			if (StringUtils.isEmpty(apassword)) {
				req.setAttribute("error", "密码为空");
				doGet(req, resp);
				return;
			}
			if (StringUtils.isEmpty(code)) {
				req.setAttribute("error", "验证码为空");
				doGet(req, resp);
				return;
			}
			if(!yan_code.equals(code)){
				req.setAttribute("error", "验证码错误");
				doGet(req, resp);
				return;
			}
			AdminDao ad = new AdminDao();
			Admin_he admin = ad.check(aname, apassword);
			
			if (admin == null) {
				req.setAttribute("error","!用户名或者密码错误,查无此人 ");
				doGet(req, resp);
				return;
			}else{
				HttpSession session=req.getSession();
				session.setAttribute("admininfo", admin);
				session.setMaxInactiveInterval(3*60*60*60);
				session.setAttribute("adminname", admin.getAname());
				resp.sendRedirect(req.getContextPath() + "/houtaiview");
			}
		} catch (Exception e) {
			e.printStackTrace();

		}

	}

}
